//
//  ThirdViewController.m
//  百度地图
//
//  Created by ljm on 2017/7/6.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()<BMKGeoCodeSearchDelegate,BMKLocationServiceDelegate>

{
    BMKMapView *mapView;
    BMKGeoCodeSearch *_searcher;
    BMKLocationService *_locationServer;
}

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self loadMapView];
    _searcher = [[BMKGeoCodeSearch alloc] init];
    _searcher.delegate = self;
    
    
    _locationServer = [[BMKLocationService alloc] init];
    _locationServer.delegate = self;
    
    /** 启动 **/
    [_locationServer startUserLocationService];
    
    /** 显示定位图层 **/
    mapView.showsUserLocation = YES;
//    [mapView updateLocationData:_locationServer];
    
}

- (void)loadMapView
{
    self.title = @"BMKmap";
    
    mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    [mapView setTrafficEnabled:YES];
    
    
    
    [self.view addSubview:mapView];
    
    /** 使用点击事件，成功 **/
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth - 80, screenHeight - 40, 80, 40)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"search" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchReversePlace) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
//
    /** 延迟执行，失败 **/
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) 5), dispatch_get_main_queue(), ^{
//        [self searchPlace];
//    });
//
    
    /** 异步执行，失败 **/
//    dispatch_async(dispatch_get_main_queue() , ^{
//        [self searchPlace];
//    });
}


#pragma mark -- 处理位置坐标更新
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    NSLog(@"处理方向变更");
}

#pragma mark -- 处理位置变更
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"处理位置变更");
    
    [mapView updateLocationData:userLocation];
}

#pragma mark -- 反向地理编码检索
- (void)searchReversePlace
{
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(39.915, 116.404);
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
    reverseGeoCodeSearchOption.reverseGeoPoint = pt;
    
    BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
    
    if (flag) {
        NSLog(@"反geo检索发送成功");
    }else
    {
        NSLog(@"反geo检索发送失败");
    }
}

#pragma mark -- 接收反向地理编码结果
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        NSLog(@"反向地理位置搜索结果%@",searcher);
        NSLog(@"反向地理位置编码结果%@",result);
    }else
    {
        NSLog(@"抱歉，反向地理位置编码未找到结果");
    }
}


#pragma mark -- 正向地理编码检索  地名-->坐标
- (void)searchPlace
{
    
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc] init];
    geoCodeSearchOption.city = @"昆明市";
    geoCodeSearchOption.address = @"官渡区春城路219号";
    
    BOOL flag = [_searcher geoCode:geoCodeSearchOption];
    if (flag) {
        NSLog(@"geo检索发送成功");
    }else
    {
        NSLog(@"geo检索发送失败");
    }
}
#pragma mark -- 接收正向编码结果
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        NSLog(@"搜索结果%@",searcher);
        NSLog(@"检索结果%@",result);
    }else
    {
        NSLog(@"未找到结果");
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    _searcher.delegate = nil;
    _locationServer.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
